<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="100px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="菜单类型:" prop="menuType">
            <hey-select @change="changeMenuType" :clearable="false" :disabled="disableType" v-model="formModel.menuType" clcd="menu_type_cd"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="showParent">
          <el-form-item label="上级菜单:" prop="parentId">
            <el-select v-model="formModel.parentId" clearable filterable>
              <el-option v-for="item in parents" :key="item.menuId" :label="item.menuName" :value="item.menuId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单名称:" prop="menuName">
            <el-input v-model="formModel.menuName" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="showPath">
          <el-form-item label="菜单路径:" prop="menuPath">
            <el-input v-model="formModel.menuPath" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="showComponent">
          <el-form-item label="组件路由:" prop="menuComponent">
            <el-input v-model="formModel.menuComponent" :disabled="disableComponent" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="showPermissionCode">
          <el-form-item label="权限码:" prop="permissionCode">
            <el-input v-model="formModel.permissionCode" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示顺序:" prop="sortNum">
            <el-input-number v-model="formModel.sortNum" align="left" controls-position="right" :max="999" :min="1" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  export default {
    name: 'MenuMnt',
    props: ['pageType', 'editData'],
    data() {
      return {
        showParent:false,//true显示 false不显示
        disableComponent:true,//true屏蔽 false可用
        disableType:false,
        showPermissionCode:false,
        showPath:true,
        showComponent:true,
        parents:[],
        oneMenus:[],
        oneAndTwoMenus:[],
        threeMenus:[],
        formModel: {
          menuId: '',
          menuType: '1',
          parentId: '',
          menuName: '',
          menuPath: '',
          menuComponent: '',
          permissionCode: '',
          sortNum: '',
        },
        rules: {
        }
      }
    },
    computed: {},
    mounted() {
      this.init()
      this.getParents()
    },
    methods: {
      init() {
        this.$form = this.$refs.form
        const { pageType, editData, formModel } = this
        if (pageType === 'edit') {
          this.assignData(editData, formModel)
          this.disableType = true
        }
        this.initRules()
      },
      initRules() {
        const REQUIRED_MSG = this.CONST.REQUIRED_MSG
        this.rules = {
          menuType: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
          parentId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
          menuName: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
          menuPath: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
          menuComponent: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
          permissionCode: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
          sortNum: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        }
        this.$nextTick(_ => {
          this.$form.clearValidate()
        })
      },
      getParents(){
        const param = {pageNum:1,pageSize:9999}
        param.isDelete = '0'
        this.get('/menu/queryAllMenus', param).then(res => {
          this.oneMenus = res.list.filter(item => item.menuType === '1')
          this.threeMenus = res.list.filter(item => item.menuType === '3')
          this.oneAndTwoMenus = res.list.filter(item => item.menuType === '1' || item.menuType === '2')
          this.changeMenuType(this.formModel.menuType)
          this.init()
        })
      },
      changeMenuType(value){
        if(value=='1'){
          //主目录
          this.showParent = false//true显示 false不显示
          this.formModel.parentId = ''
          this.showComponent = true
          this.disableComponent = true//true屏蔽 false可用
          this.formModel.menuComponent = '/layout'
          this.showPermissionCode = false
          this.formModel.permissionCode = ''
          this.showPath = true
          this.parents = []
        }else if(value=='2'){
          //次目录
          this.showParent = true//true显示 false不显示
          this.disableComponent = true//true屏蔽 false可用
          this.showComponent = false
          this.formModel.menuComponent = ''
          this.showPermissionCode = false
          this.formModel.permissionCode = ''
          this.showPath = true
          this.formModel.parentId = ''
          this.parents = []
          this.parents = this.oneMenus
        }else if(value=='3'){
          //菜单
          this.showParent = true//true显示 false不显示
          this.showComponent = true
          this.formModel.menuComponent = ''
          this.disableComponent = false//true屏蔽 false可用
          this.showPermissionCode = false
          this.formModel.permissionCode = ''
          this.showPath = true
          this.formModel.parentId = ''
          this.parents = []
          this.parents = this.oneAndTwoMenus
        }else if(value=='4'){
          //资源
          this.showParent = true//true显示 false不显示
          this.showComponent = false
          this.formModel.menuComponent = ''
          this.showPermissionCode = true
          this.showPath = false
          this.formModel.menuPath = ''
          this.formModel.parentId = ''
          this.parents = []
          this.parents = this.threeMenus
        }
      },
      handleSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            const { pageType, formModel } = this
            const param = { ...formModel }
            const operateType = pageType === 'edit' ? '02' : '01'
            param.operateType = operateType
            const url = '/menu/maintainMenu'
            const tip = pageType === 'edit' ? '修改成功！' : '新增成功！'
            this.post(url, param).then(res => {
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        })
      },
      closeDialog() {
        this.$parent.$emit('update:visible', false)
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
